'''
Request_FZ为封装发起http请求，FZ为封装的简称
'''

import requests
from loguru import logger
import traceback
from TongYong.Decode_dict import Decode_dict


class Request_info:
    def __init__(self,url,params=None,data=None,headers=None,files=None,json=None,timeout=1):
        self.url = url
        self.params = params
        self.data = data
        self.headers = headers
        self.files = files
        self.json = json
        self.timeout = timeout


    def __Request_info(self,method):
        '''
        将method（即请求方式）做为一个参数传进来
        :param method:
        :return:返回调用Decode_dict解析数据后的内容
        '''
        logger.debug("url:{},params:{},data:{},json:{},files:{},headers:{}", self.url, self.params, self.data,
                   self.json, self.files, self.headers)
        #  logger.log 表示先打印出传入的参数内容
        try:
            self.response_info = requests.request(method = method,url = self.url,params = self.params,data = self.data,
                                             json = self.json,files = self.files,headers = self.headers,timeout = self.timeout,
                                             verify = False)
            # 调用requests.request库进行http请求，并获取到对应的数据
            print("ceshi")
        except:
            logger.error("接口不通，请确认相关信息：",traceback.format_exc())
            return Decode_dict()  #返回一个空字典，因为logger.error出现的时候，表示请求未能获取到正确的数据

        return self.__get_response()   # 返回调用self.__get_response()所得到用Decode_dict解析数据后的内容


    def __get_response(self):
        try:
            get_response = self.response_info.json()  #调用json方法进行数据json数据解析
        except:
            logger.error("返回的数据内容异常，不能正常获取！Http_status：{},response:{}",self.response_info.status_code,
                         self.response_info.content)
            return Decode_dict()

        return Decode_dict(get_response)

    def post(self):
        return self.__Request_info("post")

    def get(self):
        return self.__Request_info("get")

    def delete(self):
        return self.__Request_info("del")

    def put(self):
        return self.__Request_info("put")